<?php
require('functions/page_load.php');

$settings_nav = true;
$active_tab = 'details';

$results = ($_GET['new'] == 'true') ? '<span class="success_result" style="margin-bottom: 10px;">Banner successfully created.</span>' : $results ;

if(isset($_GET['delete_trade'])){

		$active_tab = 'trades';

		$sql = "DELETE FROM banners_to_trades WHERE id = '".mysql_real_escape_string($_GET['delete_trade'])."'";
		$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());

		$results = '<div class="success_result">Banner deleted from trade successfully!</div>';

}


if($_SERVER['REQUEST_METHOD'] == 'POST'){

	if(isset($_POST['add_trade'])){
	
		$active_tab = 'trades';
	
		$sql = "INSERT INTO banners_to_trades SET banner_id = '".mysql_real_escape_string($_GET['id'])."', trade_id = '".mysql_real_escape_string($_POST['trade_id'])."'";
		$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());

		$results = '<div class="success_result">Banner added to trade successfully!</div>';
	
	}

	if(isset($_POST['edit_details'])){
	
	if(strlen($_FILES['image_file']['tmp_name']) > 0){
	move_uploaded_file($_FILES['image_file']['tmp_name'], '../images/banners/'.basename($_FILES['image_file']['name']));
	$image = "img = '".basename($_FILES['image_file']['name'])."',";
	}
	
	$sql = "UPDATE banners SET 
			".$image."
			code = '".mysql_real_escape_string($_POST['code'])."',
			link = '".mysql_real_escape_string($_POST['link'])."',
			placement = '".mysql_real_escape_string($_POST['placement'])."',
			published = '".mysql_real_escape_string($_POST['published'])."'
			WHERE id = '".mysql_real_escape_string($_GET['id'])."'";
	$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
	$results = '<div class="success_result">Banner updated successfully!</div>';
		
	}

}

$sql = "SELECT img, code, link, placement, published FROM banners WHERE id = '".mysql_real_escape_string($_GET['id'])."'";
$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
$rs = mysql_fetch_assoc($query);

require('includes/header.php');
?>

<div class="side_container">
<h2 class="container_header">Navigation</h2>

<ul>
<li><a href="banners.php">Banners</a></li>
<li><a href="trades.php">Trades</a></li>
<li><a href="directory_categories.php">Directory Categories</a></li>
<li><a href="subscriptions.php">Subscriptions</a></li>
<li><a href="sms_packages.php">SMS Packages</a></li>
</ul>

</div>

<div class="main_container_wrapper">

	<div class="main_container">
	<h2 class="container_header">Edit Banner</h2>
	
	<div id="top_links">
	<a class="tab_link" rel="#trades" style="background-image: url(images/list.png); padding-left: 20px;">Trades</a>
	<a class="tab_link" rel="#details" style="background-image: url(images/page.png); padding-left: 20px;">Banner details</a>
	<a href="banners.php" style="background-image: url(images/back.png); padding-left: 20px;">Back to Banners</a>
	</div>
	
	<?php echo $results; ?>
	
	<div id="details" class="tab_container"<?php echo ($active_tab == 'details') ? ' style="display: block;"' : ' style="display: none;"' ; ?>>
	
			<form method="post" action="" enctype="multipart/form-data">
			
			<input type="hidden" name="edit_details" value="true" />
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="image_file">Image:</label></div>
			<div class="form_row_right"><input type="file" name="image_file" id="image_file" /><?php echo (strlen($rs['img']) > 0) ? '<br /><a href="/images/banners/'.$rs['img'].'" rel="prettyPhoto" style="color: #333333; font-size: 11px;">Current image</a>' : '' ; ?><br /><span style="font-size: 11px;">Image must be 468x60 for header banners or 300x250 for column banners.</span></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="code">Code:</label></div>
			<div class="form_row_right"><textarea name="code" id="code" style="width: 250px;"><?php echo $rs['code']; ?></textarea></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="link">Link:</label></div>
			<div class="form_row_right"><input type="text" name="link" id="link" value="<?php echo $rs['link']; ?>" style="width: 250px;" /></div>
			</div>

			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="placement_header">Display in:</label></div>
			<div class="form_row_right"><input type="radio" name="placement" id="placement_header" value="header"<?php echo ($rs['placement'] == 'header') ? ' checked="checked"' : '' ; ?> /> <label for="placement_header"><span>Header</span></label><br /><input type="radio" name="placement" id="column" value="column"<?php echo ($rs['placement'] == 'column') ? ' checked="checked"' : '' ; ?> /> <label for="column"><span>Column</span></label></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="published">Published:</label></div>
			<div class="form_row_right"><input type="checkbox" name="published" id="published" value="1"<?php echo ($rs['published'] == '1') ? ' checked="checked"' : '' ; ?> /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left">&nbsp;</div>
			<div class="form_row_right"><input type="image" src="images/save_button.png" alt="Save" /></div>
			</div>
			
			</form>
	</div>
			
	<div id="trades" class="tab_container"<?php echo ($active_tab == 'trades') ? ' style="display: block;"' : ' style="display: none;"' ; ?>>
			
	
	<table width="100%" cellpadding="0" cellspacing="0" class="cms_table">
		<thead><tr><th align="left">Trade</th><th width="50">Delete</th></tr></thead>
		<tbody>
		<?php
		
		$sql = "SELECT banners_to_trades.id, trade FROM banners_to_trades 
				LEFT JOIN trades ON banners_to_trades.trade_id = trades.id 
				WHERE banners_to_trades.banner_id = ".mysql_real_escape_string($_GET['id'])."";
		$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
		$rows = mysql_num_rows($query);
		
		if($rows > 0){
		
			while($rs=mysql_fetch_assoc($query)){
			
			echo '<tr><td align="left" valign="top">'.$rs['trade'].'</td><td valign="top"><a onclick="confirm_delete(\'?id='.$_GET['id'].'&amp;delete_trade='.$rs['id'].'\',\'this trade from this banner\');" title="Delete"><img src="images/delete.png" alt="Delete" /></a></td></tr>';
			
			}
			
		}
		
		else{
		
		echo '<tr><td colspan="2">There are currently no trades for this banner.</td></tr>';
		
		}
		
		?>
		
		</tbody></table>
		
			<form method="post" action="">
			
			<input type="hidden" name="add_trade" value="true" />

			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="trade_id">Add trade:</label></div>
			<div class="form_row_right"><select name="trade_id" id="trade_id">
			<?php
			
			$sql = "SELECT id, trade FROM trades ORDER BY trade";
			$trades_query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
			while($trades_rs = mysql_fetch_assoc($trades_query)){
			
				echo '<option value="'.$trades_rs['id'].'">'.$trades_rs['trade'].'</option>';
			
			}
			
			?>
			</select></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left">&nbsp;</div>
			<div class="form_row_right"><input type="image" src="images/save_button.png" alt="Save" /></div>
			</div>
			
			</form>
			
			
	</div>
			

	</div>

</div>
	
<?php
	require('includes/footer.php');
?>